using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using Impact.Infrastructure.Plugins;
using Impact.Infrastructure;

namespace Impact.Plugins
{

    /// <summary>
    /// Base class for Plugin Admin components.  Contains typical functionality for admins.
    /// </summary>
    public class PluginAdminBase : Impact.Infrastructure.Plugins.IPluginAdmin
    {

        #region IPluginAdmin Members

        /// <summary>
        /// Returns the correct control to be used to edit the given plugin parameter
        /// </summary>
        public virtual Control GetParameterEditControl( PluginParameter parameter )
        {
            Control output;

            if ( parameter.Type == typeof( bool ) )
            {
                CheckBox checkBox = new CheckBox();
                checkBox.Checked = Utility.StringAsBool( parameter.Value );
                output = checkBox;
            }
            else
            {
                TextBox textBox = new TextBox();
                textBox.Text = parameter.Value;
                output = textBox;
            }

            return output;
        }

        /// <summary>
        /// get the value of the given control
        /// </summary>
        public string GetControlValue( Control control )
        {
            if ( control is DropDownList )
                return ( (DropDownList)control ).SelectedValue;
            else if ( control is TextBox )
                return ( (TextBox)control ).Text;
            else if ( control is CheckBox )
                return ( (CheckBox)control ).Checked.ToString();
            else
                throw new NotImplementedException( string.Format( "This control type not implemented: {0}", control.ToString() ) );
        }

        #endregion
    }
}
